{extend name='admin@main'}

{block name="content"}

<script>
layui.use('upload', function(){
  var $ = layui.jquery
  ,upload = layui.upload;
  

  
	//多文件列表示例
	var demoListView = $('#demoList'),
	uploadListIns = upload.render({
		elem: '#testList',
		url: '{:url("upload")}',
		accept: 'file',
		multiple: true,
		auto: false,
		bindAction: '#testListAction',
		choose: function(obj) {
			var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列
			//读取本地文件
			obj.preview(function(index, file, result) {
			
				//console.log(index);
				
				var tr = $(['<tr id="upload-' + index + '">', '<td>' + file.name + '</td>', '<td>' + (file.size / 1014).toFixed(1) + 'kb</td>', '<td>等待上传</td>', '<td>', '<button class="layui-btn layui-btn-xs demo-reload layui-hide">重传</button>', '<button class="layui-btn layui-btn-xs layui-btn-danger demo-delete">删除</button>', '</td>', '</tr>'].join(''));

				//单个重传
				tr.find('.demo-reload').on('click',
				function() {
					obj.upload(index, file);
				});

				//删除
				tr.find('.demo-delete').on('click',
				function() {
					delete files[index]; //删除对应的文件
					tr.remove();
					uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值，以免删除后出现同名文件不可选
				});

				demoListView.append(tr);
			});
		},
		done: function(res, index, upload) {
			console.log(res);
			
			
			//console.log(upload);
			if (res.code == 0) { //上传成功
				var tr = demoListView.find('tr#upload-' + index),
				tds = tr.children();
				var str ='<input type="hidden" name="fileurl[]" value="'+res.url+'"><input type="hidden" name="user_id[]" value="'+res.user_id+'">';
				str +='<input type="hidden" name="original_name[]" value="'+res.original_name+'"><input type="hidden" name="fileext[]" value="'+res.extension+'">';
				str +='<input type="hidden" name="filesize[]" value="'+res.size+'"><input type="hidden" name="filename[]" value="'+res.save_name+'">';
				str +='<input type="hidden" name="md5code[]" value="'+res.md5+'"><input type="hidden" name="sha1code[]" value="'+res.sha1+'">';
				
				tds.eq(2).html('<span style="color: #5FB878;">上传成功'+str+'</span>');
				tds.eq(3).html('<a href="'+res.url+'" target="new">查看</a>'); //清空操作
				return delete this.files[index]; //删除文件队列已经上传成功的文件
			}
			this.error(index, upload);
		},
		error: function(index, upload) {
			var tr = demoListView.find('tr#upload-' + index),
			tds = tr.children();
			tds.eq(2).html('<span style="color: #FF5722;">上传失败</span>');
			tds.eq(3).find('.demo-reload').removeClass('layui-hide'); //显示重传
		}
	});
  
  //绑定原始文件域
  upload.render({
    elem: '#test20'
    ,url: '/upload/'
    ,done: function(res){
      console.log(res)
    }
  });
  
});
</script>




<form class="layui-form layui-card" action="{:request()->url()}"  onsubmit="return false;" data-auto="true" method="post" autocomplete="off" id="Newform" lay-filter="component-form-group">

    <div class="layui-card-body think-box-shadow padding-left-40">
	


        <div class="layui-form-item">
            <label class="layui-form-label ">文件标题</label>
            <div class="layui-input-block">
				<input name="title" value='{$vo.title|default=""}' required placeholder="请输入文件标题" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item nowrap">
            <label class="layui-form-label label-required">拟用公章</label>
            <div class="layui-input-inline margin-right-0" style="width:120px">
				<select class="layui-select" name="gonggaoleixing" lay-verify="required" lay-search>
					{foreach [''=>'-- 选择 --','0'=>'某某XX公司公章','1'=>'法人私章','2'=>'财务公章','3'=>'工会公章','4'=>'XX公章','5'=>'党支部公章'] as $k=>$v}
					{eq name='$vo.gonggaoleixing' value='$k.""'}
					<option selected value="{$k}">{$v}</option>
					{else}
					<option value="{$k}">{$v}</option>
					{/eq}
					{/foreach}
				</select>
            </div>
			
            <label class="layui-form-label">经办人</label>
            <div class="layui-input-inline margin-right-0" style="width:120px">
				{if isset($vo) and isset($vo.docauthor)}
                <input disabled value='{$vo.docauthor|default=""}' class="layui-input layui-bg-gray">
                {else}
		<input name="docauthor" value='{$vo.docauthor|default=session('admin_user.nickname')}' placeholder="请输入经办人" class="layui-input">
				{/if}
            </div>

            <label class="layui-form-label">用章日期</label>
            <div class="layui-input-inline margin-right-0" style="width:120px">
		<input type="text" data-date-input="date" name="docdate" value='{$vo.docdate|format_datetime='Y-m-d'|default=""}' lay-verify="required|date"  placeholder="用章日期" class="layui-input" >
            </div>
			
        </div>


        <div class="layui-form-item nowrap">
            <label class="layui-form-label label-required">文件份数</label>
            <div class="layui-input-inline margin-right-0" style="width:120px">
				<input name="doccount" value='{$vo.doccount|default="0"}'  lay-verify="number|required"  placeholder="请输入文件份数" class="layui-input">
            </div>
			
            <label class="layui-form-label">盖章个数</label>
            <div class="layui-input-inline margin-right-0" style="width:120px">
	<input type="text" name="stampcount" value='{$vo.stampcount|default='0'}' lay-verify="required|number"  placeholder="请输入盖章个数" class="layui-input" >
            </div>

            <label class="layui-form-label">是否借出</label>
            <div class="layui-input-inline margin-right-0" style="width:120px">
				<select class="layui-select" name="isout" lay-verify="required" lay-search>
					<option  value="">--请选择--</option>
					{foreach ['0'=>'是','1'=>'否'] as $k=>$v}
					{eq name='$vo.isout' value='$v'}
					<option selected value="{$v}">{$v}</option>
					{else}
					<option value="{$v}">{$v}</option>
					{/eq}
					{/foreach}
				</select>
            </div>
			
        </div>


        <div class="layui-form-item nowrap">
            <label class="layui-form-label label-required">外借时间</label>
            <div class="layui-input-inline margin-right-0" style="width:120px">
			  <input data-date-range name="outdate" id="outdate" value="{$vo.outdate|default=''}" placeholder="请选择申请借出时间" class="layui-input">
            </div>
			
            <label class="layui-form-label">归还时间</label>
            <div class="layui-input-inline margin-right-0" style="width:120px">
			  <input data-date-range name="returndate" id="returndate" value="{$vo.returndate|default=''}" placeholder="请选择申请归还时间" class="layui-input">
            </div>


			
        </div>

		
        <div class="layui-form-item">
            <label class="layui-form-label ">备   注</label>
            <div class="layui-input-block">
				<input name="remark" value='{$vo.remark|default=""}' placeholder="备   注" class="layui-input">
            </div>
        </div>


		
        <div class="layui-form-item">
            <label class="layui-form-label ">科室负责人</label>
            <div class="layui-input-block">
                <div id="keshifuzeren" class="xm-select-demo"></div>
            </div>
        </div>


        <div class="layui-form-item">
            <label class="layui-form-label ">主要领导</label>
            <div class="layui-input-block">
				<div id="fenguanlingdao" class="xm-select-demo"></div>
            </div>
        </div>
		

		
        <div class="layui-form-item">
            <label class="layui-form-label ">印章保管人</label>
            <div class="layui-input-block">
				<div id="shouwenwenyuan" class="xm-select-demo"></div>
            </div>
        </div>

		{if request()->action() =='edit'}
        <div class="layui-form-item">
            <label class="layui-form-label ">批示情况</label>
            <div class="layui-input-block">
                <div class="layui-textarea padding-bottom-10" style="min-height:auto">

						<table class="layui-table">
						  <thead>
							<tr>
							<th>批示人</th>
							<th>批示时间</th>
							<th>批示类型</th>
							<th width="164px">批示内容</th>
						  </tr></thead>
						  {foreach $list_flow as $key=>$vo}
						  <tr id="upload-{$vo.id}">
						  <td>{$vo.username}</td>
						  <td>{$vo.creat_at|format_datetime='Y-m-d H:i'}</td>
						  
						  <td>                
						  {eq name='vo.types' value='nigao'}<span class="layui-bg-red">发起申请</span>{/eq}
						  {eq name='vo.types' value='keshifuzeren'}<span class="layui-bg-orange">科室/项目负责人审核</span>{/eq}
						  {eq name='vo.types' value='shouwenwenyuan'}<span class="layui-bg-green">印章保管人</span>{/eq}
						  {eq name='vo.types' value='bangongshizhuren'}<span class="layui-bg-blue">办公室主任</span>{/eq}
						  {eq name='vo.types' value='fenguanlingdao'}<span class="layui-bg-black">主要领导</span>{/eq}
						  {eq name='vo.types' value='zhuyaolingdao'}<span class="layui-bg-gray">主要领导</span>{/eq}
						  </td>
						  
						  <td> {$vo.info}</td>  <tr>
						  {/foreach}
						</table>

                </div>
            </div>
        </div>
		{/if}


		{if request()->action() =='edit' && $rs_flow >0 && $my_flow_count ==0 }
        <div class="layui-form-item">
            <label class="layui-form-label">填写意见</label>
            <div class="layui-input-block">
				<textarea class="layui-textarea" name="info" id="info" placeholder="请填写批示意见">{$info|default=""}</textarea>
            </div>
			
        </div>
		{/if}

        <div class="layui-form-item text-center">
            {notempty name='id'}<input type="hidden" name="id" id="id" value="{$id}">{/notempty}

			<input type='hidden' value='{$docauthor}' name='docauthor'>
			{if request()->action() =='edit' && $rs_flow >0 && $my_flow_count ==0 }
			<button class="layui-btn" type='button' id="saveinfo">保存批示意见</button>
			{/if}
			
			{if request()->action() =='edit' && $docauthor == session('admin_user.nickname')  }
			<button class="layui-btn" type='submit'>流转文件</button>
			{/if}
			
			{if request()->action() =='add' }
			<button class="layui-btn" type='submit' lay-filter="formDemo" >保存数据</button>
			{/if}
			<button class="layui-btn layui-btn-danger" onclick="hsitoryBack()" type="button">关闭</button>
        </div>

    </div>
</form>
{/block}

{block name='script'}


	<script src="__ROOT__/static/plugs/select/xm-Select.js"></script>

<script>
    window.form.render();
	function hsitoryBack(){
		$.msg.confirm('确定要关闭吗？', function (index) {
			history.back(), $.msg.close(index);
		});
	};
	
	//自定义日期格式
	laydate.render({ 
	  elem: '#outdate'
	  ,type: 'datetime'
	  ,format: 'yyyy年MM月dd日 HH时' //可任意组合
	  //,range: true
	});
   
	//自定义日期格式
	laydate.render({ 
	  elem: '#returndate'
	  ,type: 'datetime'
	  ,format: 'yyyy年MM月dd日 HH时' //可任意组合
	  //,range: true
	});
   
	
    //require(['ckeditor', 'angular','plugs/select/xm-Select'], function () {
        //window.createEditor('[name="content"]', {height: 250});
		//var app = angular.module("Newform", []).run(callback);
		
		function callback($rootScope) {
            // 返回列表
            $rootScope.hsitoryBack = function () {
                $.msg.confirm('确定要取消编辑吗？', function (index) {
                    history.back(), $.msg.close(index);
                });
            };
		}
		
		
		
		
		var keshifuzeren = xmSelect.render({
			el: '#keshifuzeren', 
			name: 'keshifuzeren',
			toolbar: {
				//show: true,
				//list: [ 'ALL', 'CLEAR', 'REVERSE' ]
			},

			theme: {
				color: '#0081ff',
			},			
			paging: true,
			pageSize: 10,
			radio: true,
			filterable: true,
			data: [
				{$str}
			]
		})


		
		var fenguanlingdao = xmSelect.render({
			el: '#fenguanlingdao', 
			filterable: true,
			name: 'fenguanlingdao',
			paging: true,
			pageSize: 10,
			toolbar: {
				show: true,
				list: [ 'ALL', 'CLEAR', 'REVERSE' ]
			},
			theme: {
				color: '#8dc63f',
			},
			model: {
				label: {
					type: 'block',
					block: {
						//最大显示数量, 0:不限制
						showCount: 3,
						//是否显示删除图标
						showIcon: true,
					}
				}
			},
			data: [
				{$str}
			]
		})
		
		var shouwenwenyuan = xmSelect.render({
			el: '#shouwenwenyuan', 
			name: 'shouwenwenyuan',
			toolbar: {
				show: true,
				list: [ 'ALL', 'CLEAR', 'REVERSE' ]
			},
			paging: true,
			pageSize: 10,
			filterable: true,
			theme: {
				color: '#e54d42',
			},
			model: {
				label: {
					type: 'block',
					block: {
						//最大显示数量, 0:不限制
						showCount: 3,
						//是否显示删除图标
						showIcon: true,
					}
				}
			},

			data: [
				{$str}
			]
		})
		

		
		{if isset($vo) }
		keshifuzeren.setValue([ {$keshifuzeren} ])
			
		fenguanlingdao.setValue([ {$fenguanlingdao} ])
		
		shouwenwenyuan.setValue([ {$shouwenwenyuan} ])
		{/if}
    //})
</script>

<script>
    layui.use(['layer','form' ,'jquery','layedit'], function() {
        var layer = layui.layer, $ = layui.jquery ,form = layui.form;
        var layedit =layui.layedit;



	  //监听指定开关
	  /*
	  form.on('switch(switchTest)', function(data){
	  	console.log(data)
	    //layer.msg('开关checked：'+ (this.checked ? 'true' : 'false'), {
	    //  offset: '6px'
	    //});
	    //layer.tips('温馨提示：如果是借出，请选择借出和归还时间', data.othis)
        //开关是否开启，true或者false
        var checked = data.elem.checked;
        //获取所需属性值
        var switch_goods_id = data.elem.attributes['switch_goods_id'].nodeValue;
        //console.log(checked);
        console.log(switch_goods_id)
        {if $vo.isout eq '是'}
        data.elem.checked = checked;
        {else}
        data.elem.checked = !checked;
        {/if}
         form.render();
	  });
		

	
        form.on('radio(isout)', function (data) {

        	
		

			console.log(data);

			if(data.value =='是')
			{
				//console.log(data)
				form.verify({
				  returndate: function(value, item){ //value：表单的值、item：表单的DOM对象
				  	console.log(value);
                    if (value.length < 4) {
                        alert( '请选择借出日期' );
                        return ;
                    }
				  }
				}); 
			}

            //alert(data.elem);
            //console.log(data.elem);
            //alert(data.value);//判断单选框的选中值
            //console.log(data.value);
            //alert(data.othis);
           // layer.tips('开关checked：' + (this.checked ? 'true' : 'false'), data.othis);
            //layer.alert('响应点击事件');
        });
		*/



        //监听提交
        form.on('submit(formDemo)', function(data){

	        //自定义验证规则
	        form.verify({
	            title: function (value, item) {
	                if (value.length < 5) {
	                    return '标题至少得5个字符啊';
	                }
	            },
	            remark: function (value) {
	                if (value.length < 4) {
	                    return '内容请输入至少4个字符';
	                }
	            }
	            , doccount: [/^[0-9]*[1-9][0-9]*$/, '必须是数字！']
	            , stampcount: [/^[0-9]*[1-9][0-9]*$/, '必须是数字！']
	            //, phone: [/^1[3|4|5|7|8]\d{9}$/, '手机必须11位，只能是数字！']
	            //, email: [/^[a-z0-9._%-]+@([a-z0-9-]+\.)+[a-z]{2,4}$|^1[3|4|5|7|8]\d{9}$/, '邮箱格式不对']
	        });





        });

        form.render();
    });
	

	$('#saveinfo').click(function(){
		var info = $("#info").val();
		var docid =$("#id").val();

		
		if(info == ''){
			layer.msg('请填写批示意见!',{icon: 5});//失败的表情
			return ;		
		}
		
		//alert(info);
		
		var url = "/document/stamp/saveinfo.html";
		$.ajax({
			url:url,
			type:'post',
			data:'info='+info+'&docid='+docid,
			beforeSend:function () {
				
				this.layerIndex = layer.load(0, { shade: [0.5, '#393D49'] });
			},
			success:function(data){
				if(data.status == 'error'){
					layer.msg(data.msg,{icon: 5});//失败的表情
					return;
				}else if(data.status == 'success'){
					//layer.msg(data.msg,{icon: 6});
					layer.msg(data.msg, {
						icon: 6,//成功的表情
						time: 2000 //1秒关闭（如果不配置，默认是3秒）
					}, function(){
						//location.reload();
						window.location.href="/admin.html#/document/stamp/index.html?spm=m-77-81-82";
					});
				}
			},
			complete: function () {
				layer.close(this.layerIndex);
			},
		});
		return false;//阻止表单跳转
	});














/***** HisiPHP By http://www.HisiPHP.com *****/
layui.define(['jquery', 'form'], function(exports) {
    var $ = layui.jquery, form = layui.form;
    var obj = {
        assign: function(formData) {
            var input = '', form = layui.form;
            for (var i in formData) {
                switch($('.field-'+i).attr('type')) {
                    case 'select':
                        input = $('.field-'+i).find('option[value="'+formData[i]+'"]');
                        input.prop("selected", true);
                        break;
                        
                    case 'radio':
                        input = $('.field-'+i+'[value="'+formData[i]+'"]');
                        input.prop('checked', true);
                        break;

                    case 'checkbox':
                        if (typeof(formData[i]) == 'object') {
                            for(var j in formData[i]) {
                                input = $('.field-'+i+'[value="'+formData[i][j]+'"]');
                                input.prop('checked', true);
                            }
                        } else {
                            input = $('.field-'+i+'[value="'+formData[i]+'"]');
                            input.prop('checked', true);
                        }
                        break;

                    case 'img':
                        if (formData[i]) {
                            input = $('.field-'+i);
                            input.attr('src', formData[i]);
                        }
                        break;

                    default:
                        input = $('.field-'+i);
                        input.val(formData[i]);
                        break;
                }
                
                if (input.attr('data-disabled')) {
                    input.prop('disabled', true);
                }

                if (input.attr('data-readonly')) {
                    input.prop('readonly', true);
                }
            }
            form.render();
        },
    };

    exports('func', obj);
}); 
</script>
{/block}